const app = getApp();
const {htmlDecode} = require('../../utils/util.js')
const api = require("../../utils/apiInstance");
const {getUserInfo} = require("../../utils/store");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        bigSearchShow: false,
        bigSearchType: "",
        searchDataList: [],
        dutyDept: '',
        fileUrl: app.globalData.fileUrl,
        risks: [],
        areaList: [],
        areaIndex: '',
        sapId: null,
        myName: null,
        proposeName: '',
        dealFlag: 1,
        nodeArray: ['申请退回', '指派责任部门', '指派处理', '隐患排除中', '整改后审核', '闭环'],
        flowTypeArray: ['指派责任部门', '指派整改人', '整改隐患', '整改后审核', '延期审核', '提出隐患', '申请延期'],
        nodeIndex: '',
        baseUrl: app.globalData.baseUrl,
        x: 10, //添加的拖拽X轴
        y: 400 //添加的拖拽Y轴
    },
    cancelSelection() {
        this.setData({
            areaIndex: ''
        })
    },
    setProposeName(e) {
        this.setData({
            proposeName: e.detail.value
        })
    },
    doBigSearchShow(e) {
        console.log('e', e);
        console.log(this.data.deptList.length);
        const tempArray = [];
        const typeMethods = {
            dept: () => {
                if (this.data.deptList.length == 0) {
                    wx.showToast({
                        title: '暂无部门',
                        icon: 'none'
                    });
                    return;
                } else {
                    this.data.deptList.forEach(item => {
                        tempArray.push({
                            name: item
                        });
                    });
                }
            }
        }
        typeMethods[e.currentTarget.dataset.type]();

        this.setData({
            bigSearchType: e.currentTarget.dataset.type,
            bigSearchShow: true,
            searchDataList: tempArray
        })
    },
    searchSelect(e) {
        this.setData({
            dutyDept: e.detail.name || ""
        });
    },
    getAllDept: function () {
        var that = this;
        wx.request({
            url: that.data.baseUrl + '/tissueDept/wx/getAllDept',
            method: 'GET',
            header: {
                'Content-Type': 'application/json' // 请求头
            },
            success(res) {
                console.log('获取所有部门信息:', res.data);
                var data = res.data;
                that.setData({
                    deptList: data.data
                })
            }
        });
    },

    areaChange: function (e) {
        this.setData({
            areaIndex: e.detail.value
        })
    },

    addTouchStart: function (e) {

        this.startX = e.touches[0].pageX;
        this.startY = e.touches[0].pageY;
    },
    addTouchMove: function (e) {
        const moveX = e.touches[0].pageX;
        const moveY = e.touches[0].pageY;
        const dx = moveX - this.startX;
        const dy = moveY - this.startY;

        this.setData({
            x: this.data.x + dx,
            y: this.data.y + dy
        });

        this.startX = moveX;
        this.startY = moveY;
    },

    initSapId: function () {
        const userInfo = wx.getStorageSync('userinfo');
        this.setData({
            sapId: userInfo.siocSapNo,
            myName: userInfo.siocCnName
        })
    },
    nodeChange(e) {
        this.setData({
            nodeIndex: e.detail.value
        });
    },
    nodeCancel() {
        this.setData({
            nodeIndex: ''
        });
    },
    loadData() {
        if (this.data.dealFlag == 2 || this.data.dealFlag == 3) {
            this.getAuditRisk(this.data.dealFlag);
        } else {
            this.getMyRisk();
        }
    },
    getMyRisk: function () {
        var that = this;
        var area = that.data.areaList[that.data.areaIndex] == undefined ? '' : that.data.areaList[that.data.areaIndex];
        wx.request({
            url: that.data.baseUrl + '/tissueRisk/wx/getMyRisk?sapId=' + that.data.sapId + '&node=' + that.data.nodeIndex + '&area=' + area + '&dutyDept=' + that.data.dutyDept,
            method: 'GET',
            header: {
                'Content-Type': 'application/json' // 请求头
            },
            success(res) {
                console.log('获取所有我提交的隐患:', res.data);
                var data = res.data;

                data.rows.forEach(item => {
                    item.content = htmlDecode(item.content)
                    item.location = htmlDecode(item.location)
                    item.suggest = htmlDecode(item.suggest)
                })

                that.setData({
                    riskList: data.rows,
                    dealFlag: 1
                })
            }
        });
    },
    getMyAuditRisk: function (e) {
        this.getAuditRisk(e.currentTarget.dataset.operation);
    },

    async getAuditRisk(dealFlag) {
        let area = this.data.areaList[this.data.areaIndex] == undefined ? '' : this.data.areaList[this.data.areaIndex];
        const result = await api.get("/tissueRisk/wx/getMyAuditRisk",{"dealFlag":dealFlag,"userSapId":this.data.sapId,"node":this.data.nodeIndex,"proposeName":this.data.proposeName,"area":area});
        this.setData({
            riskList: result.rows,
            dealFlag: dealFlag
        })
    },

    goAdd() {
        wx.navigateTo({
            url: '/pages/tissueRisk-add/tissueRisk-add',
        })
    },

    async getAllArea () {
        const result = await api.get("/tissueArea/wx/getAllArea");
        this.setData({
            areaList: result.data
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.getAllArea();
        this.initSapId();
        this.getAuditRisk(2);
        this.getAllDept();
        this.startX = 0;
        this.startY = 0;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.loadData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})